﻿using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using System;
using wProtobuf;
using Org.Xresloader.Pb;

namespace Test
{
    public class TestConfigLoader : MonoBehaviour
    {
        private void Start()
        {
            var list = Load<Protos.TestUniversalConfig>("Universal/TestUniversalConfig.bytes");
            foreach (var item in list)
            {
                Debug.Log(item.desc);
            }
        }

        /// <summary>
        /// 256KB缓冲区
        /// </summary>
        private static MessageStream s_CachedStream = new MessageStream(1024 * 256);

        public static async UniTask<List<T>> LoadAsync<T>(string overrideName = null) where T : IMessage, new()
        {
            var configFileName = overrideName;
            if (string.IsNullOrEmpty(configFileName))
                configFileName = typeof(T).Name;
            try
            {
                var textAsset = await Addressables.LoadAssetAsync<TextAsset>(configFileName).Task;
                return LoadWithTextAsset<T>(textAsset);
            }
            catch (Exception ex)
            {
                Debug.LogError($"异步加载配置文件[{configFileName}]失败，错误信息：{ex}");
                return null;
            }
        }

        public static List<T> Load<T>(string overrideName = null) where T : IMessage, new()
        {
            var configFileName = overrideName;
            if (string.IsNullOrEmpty(configFileName))
                configFileName = typeof(T).Name;
            try
            {
                var textAsset = Addressables.LoadAssetAsync<TextAsset>(configFileName).WaitForCompletion();
                return LoadWithTextAsset<T>(textAsset);
            }
            catch (Exception ex)
            {
                Debug.LogError($"同步加载配置文件[{configFileName}]失败，错误信息：{ex}");
                return null;
            }
        }

        private static List<T> LoadWithTextAsset<T>(TextAsset textAsset) where T : IMessage, new()
        {
            s_CachedStream.WritePos = 0;
            s_CachedStream.ReadPos = 0;
            s_CachedStream.Write(textAsset.bytes);
            xresloader_datablocks datablocks = new xresloader_datablocks();
            datablocks.MergeFrom(s_CachedStream);
            List<T> configs = new List<T>();
            for (int i = 0; i < datablocks.data_block.Count; i++)
            {
                s_CachedStream.WritePos = 0;
                s_CachedStream.WriteBytes(datablocks.data_block[i]);
                s_CachedStream.ReadPos = 0;
                var config = new T();
                s_CachedStream.ReadMessage(config);
                configs.Add(config);
            }
            return configs;
        }

    }
}